package com.yidu.ladder.util;


import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * ID生成器
 * @author bamboo
 *
 */
public class IDGenerator {
	
	/**
	 * 获得ID
	 * @return 返回不重复的16位字符串
	 */
	public static String getId(){
		return UUID.randomUUID()   //获取UUID
				.toString() //转为字符串
				.replace("-", "") //去除中间 -
				.toUpperCase() //转为大写
				.substring(0, 16); //取前面16个字符
	}
	
	/**
	 * 获取32位的UUID
	 * @return 由32位(数字和小写字母)组成的字符串
	 */
	public static String getUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	/**
	 * 获取订单编号
	 * @return 由18位(数字)组成的字符串
	 */
	public static String getOrderNo(){
		//创建字符缓冲区
		StringBuilder sb = new StringBuilder();
		//创建获取随机数对象
		Random random = new Random();
		//循环要获得字符长的长度的次数
		for(int i=0; i<18; i++){
			//随机数范围为0~9，添加到字符串缓冲区
			sb.append(random.nextInt(10));  
		}
		//把字符串缓存对象转为字符串并返回
		return sb.toString();
	}
	
	
	
	public static String getJobListNo() {  //GZD
		//获取请求
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    	
		//字符串头
		String noteName = "GZD";
		//设置日期格式转换为字符串
		String format = new SimpleDateFormat("yyMMdd").format(System.currentTimeMillis());
		String ip = IpUtils.getIpAddress(request);
		//
		//String str = String.format("%04d");//如果小于6位左边补0
		//将字符串拼接返回
		String jobListNo = noteName + format +ip;
		// {"nos":"1,2,3", "ip":"192.168.0.2"}
		return jobListNo;
	}
	
	
	
}
	